import {
	createStore
} from 'vuex'
import WebSocketClient from '../utils/websocket'
import { getImei, removeCurrentImei } from '../utils/user_tools'
const store = createStore({
	state: {
		language: "en",
		user: null,
		token: null,
		refreshToken: null,
		hasLogin: false,
		wsClient: null,
		homeData: null,
		liveMode:null,
		test:0,
		isClearImei:false,
		findId:null,
		modeId:null,
		fenceId:null,
		isConnectWebSocket:false,
		newImei:null,
	},
	getters: {
		newImei:(state) =>{
			return state.newImei
		},
		language: (state) => {
			return state.language
		},
		user: (state) => {
			return state.user
		},
		token: (state) => {
			return state.token
		},
		refreshToken: (state) => {
			return state.refreshToken
		},
		hasLogin: (state) => {
			return state.token ? true : false;
		},
		homeData:(state)=>{
			return state.homeData
		},
		testdata:(state)=>{
			return state.test
		},
		liveMode:(state)=>{
			return state.liveMode
		},
		findId:(state)=>{
			return state.findId;
		},
		modeId:(state)=>{
			return state.modeId;
		},
		fenceId:(state)=>{
			return state.fenceId;
		},
		isClearImei:(state)=>{
			return state.isClearImei;
		},
		wsClient:(state)=>{
			return state.wsClient;
		},
		isConnectWebSocket:(state)=>{
			return state.isConnectWebSocket;
		}
	

	},
	mutations: {
		
		setNewImei(state,newImei){
			state.newImei = newImei
		},
		setConnectWebSocket(state,isConnect){
			state.isConnectWebSocket = isConnect;
		},
		setTheme(state,color){
			
		},
		setLanguage(state, lang) {
			state.language = lang;
		},
		setUser(state, user) {
			state.user = user
		},
		setToken(state, token) {
			state.token = token
		},
		setRefreshToken(state, refreshToken) {
			state.refreshToken = refreshToken
		},
		websocketConnect(state,authorization) {
			state.isClearImei = false;
			state.wsClient = new WebSocketClient('ws://54.172.69.115:6959/socket',authorization);
			state.wsClient.connect().then(() => {
				state.isConnectWebSocket = true;
			}).catch(error => console.error('WebSocket连接失败', error));
		},
		
		webSendClose(state, message){
			state.wsClient.sendMessage(message);
		},
		webSocketSend(state, message) {
			state.wsClient.sendMessage(message);
			state.wsClient.getMessage((res) => {
				
				let dataMsg = JSON.parse(res.data)
				console.log("websocket+res==",dataMsg)
				if(dataMsg.imei == getImei() && dataMsg.type == 1 ){
					state.homeData = dataMsg.data;
				}else if(dataMsg.type == 22 && dataMsg.imei == getImei()){
					state.isClearImei = true;
					removeCurrentImei();
				}else if(dataMsg.type == 23 && dataMsg.imei == getImei()){
					state.isClearImei = true;
					removeCurrentImei();
				}else if(dataMsg.type == 30 && dataMsg.imei == getImei()){//UPLOAD
					state.modeId = dataMsg.data.id;
				}
				else if(dataMsg.type == 31 && dataMsg.imei == getImei()){//
					
				}
				else if(dataMsg.type == 32 && dataMsg.imei == getImei()){//FIND命令
					state.findId = dataMsg.data.id;
				}
				else if(dataMsg.type == 33 && dataMsg.imei == getImei()){//SOS命令
					
				}
				else if(dataMsg.type == 34 && dataMsg.imei == getImei()){//Fence命令
					state.fenceId = dataMsg.data.id;
				}
			})
		}
		
	},
	actions: {

	}
})

export default store